<?php
namespace app\admin\controller;
// use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
use think\captcha\Captcha;
use \app\admin\model\Users as m;
class Message extends Common
{
    public function index()
    {
        $pid=input('pid',0);
        $ty=input('ty',0);
        $tty=input('tty',0);
        $ttty=input('ttty',0);
        $q=input('q',"");

        $addurl="/admin/message/add";
        $pagesize=input('pagesize',0);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        if($ttty>0){
            $zid=$ttty;
        }elseif($tty>0){
            $zid=$tty;
        }elseif($ty>0){
            $zid=$ty;
        }elseif($pid>0){
            $zid=$pid;
        }
        if($pid){$map['pid']=$pid;$addurl.="/pid/".$pid;}
        if($ty){$map['ty']=$ty;$addurl.="/ty/".$ty;}
        if($tty){$map['tty']=$tty;$addurl.="/tty/".$tty;}
        if($ttty){$map['ttty']=$ttty;$addurl.="/ttty/".$ttty;}
        if($q) $map['title']=array('like',"%$q%");
        $list=Db::name("message")->where($map)->order("sendtime desc,id desc")->paginate($pagesizes);

        $model_id=get_sort_zd($zid,'model_id');//获取当前栏目模块id

        $model_fields=get_model_zd($model_id,'model_fields');
        $arr=explode(",",$model_fields);


        $this->assign("pid",$pid);
        $this->assign("ty",$ty);
        $this->assign("arr",$arr);
        $this->assign("pagesizes",$pagesizes);

        $this->assign("list",$list);
        $this->assign("addurl",$addurl);

        return $this->fetch("Message/index");
    }
    //添加
    public function add()
    {
        $id=input('id',0);
        $pid=input('pid',0);
        $ty=input('ty',0);
        $tty=input('tty',0);
        $ttty=input('ttty',0);

        if($id){
            $v=Db::name("message")->find($id);
            $pid=$v['pid'];
            $ty=$v['ty'];
            $tty=$v['tty'];
            $ttty=$v['ttty'];
        }
        $url="/admin/message/index";

        if($ttty>0){
            $zid=$ttty;
        }elseif($tty>0){
            $zid=$tty;
        }elseif($ty>0){
            $zid=$ty;
        }elseif($pid>0){
            $zid=$pid;
        }
        if($pid){$url.="/pid/".$pid;}
        if($ty){$url.="/ty/".$ty;}
        if($tty){$url.="/tty/".$tty;}
        if($ttty){$url.="/ttty/".$ttty;}
        $url.=".html";
        $v=Db::name("message")->find($id);

        if($v['id']){
            $v['pid']=$v['pid'];
            $v['ty']=$v['ty'];
            $v['tty']=$v['tty'];
            $v['ttty']=$v['ttty'];
            $v['ttyt']=$v['ttty'];
        }else{
            $v['pid']=$pid;
            $v['ty']=$ty;
            $v['tty']=$tty;
            $v['ttty']=$ttty;
            $v['id']=0;
            $v['imgnum']=0;

            $v['hits']=0;
            $v['img1']="";
            $v['img2']="";
            $v['status']=1;
            $v['model_id']=1;
            $v['pid']=$pid;
            $v['fenzhi']="";
            $v['region']="";
            $v['grade']="";
            $v['type']="";


            $v['disorder']=0;
            $v['sendtime']=time();

            $v['kfsj']="";
            $v['address']="";
            $v['phone']="";

        }


        if(Request::instance()->isPost()){
            $data=$_POST;
            if($data['id']){
//                print_r($data);
                $data['sendtime']=strtotime($_POST['sendtime']);
                $result=@Db::name("message")->update($data);
                if($result){
                    $catname=get_zd_name("catname","sort","id=".$zid);
                    $this->sys_log("编辑{$catname}栏目内容:{$v['title']}(id:$id)");
                    $this->success("修改成功",$url);

                }else{
                    $this->error("修改失败");
                }
            }else{
                $data['sendtime']=time();
                if(Db::name("message")->insert($data)){
                    $catname=get_zd_name("catname","sort","id=".$zid);
                    $this->sys_log("添加{$catname}栏目内容:{$v['title']}(id:$id)");
                    $this->success("添加成功",$url);

                }else{
                    $this->error("添加失败");
                }
            }
        }
        $v['imgnum']=get_sort_zd($zid,'imgnum')+1;
        $model_id=get_sort_zd($zid,'model_id');//获取当前栏目模块id
        $model_fields=get_model_zd($model_id,'model_fields');
        $arr=explode(",",$model_fields);
        $this->assign("arr",$arr);
        $this->assign("v",$v);


        return $this->fetch("/message/add");
    }
    //删除
    public function del()
    {
        $id=input('id',0);
        $v=Db::name("message")->find($id);
        if(Db::name("message")->delete($id)){
//        if(1){
            $url="/admin/message/index";
            if($v['pid'])$url.="/pid/".$v['pid'];
            if($v['ty'])$url.="/ty/".$v['ty'];
            if($v['tty'])$url.="/tty/".$v['tty'];
            if($v['ttty'])$url.="/ttty/".$v['ttty'];
            if(file_exists(substr($v['img1'],1))){
                unlink(substr($v['img1'],1));
            }
            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",$url);
        }else{
            $this->error("删除失败");
        }



    }


    public function action()
    {

        $act=$_POST['act'];
//        $sorting_arr=$_POST['sorting'];
        $checkid=$_POST['checkid'];
//        print_r($checkid);
//        print_r($_POST);

        // die;
        if($act=='sorting'){
            for($i=0;$i<count($checkid);$i++) {
                D()->execute("update __PREFIX__message set disorder=$sorting_arr[$i] where id='{$checkid[$i]}'");
                sys_log("信息排序更新:(id:$checkid[$i])");
            }
            $this->success("信息排序更新成功",cookie('Jumpurl'));
        }elseif($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("message")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }elseif($act=='isgood'||$act=='istop') {
            for($i=0;$i<count($checkid);$i++) {
                $v=Db::name("message")->find($checkid[$i]);
                $data[$act]=$v[$act]?0:1;
                Db::name("message")->where("id=".$checkid[$i])->update($data);
//                sys_log("删除信息:(id:$checkid[$i])");
            }
            $this->success("操作成功",cookie('Jumpurl'));
        }

    }

    //审核
    public function audit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("message")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("message")->where("id=".$id)->update($data)){
//            $this->success("审核成功","/admin/model/index.html");
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
////            $this->error("审核失败");
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }





//    事件上报
    public function reporting()
    {

        $q=input('q',"");
        $pagesize=input('pagesize',0);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        if($q) $map['title']=array('like',"%$q%");
        $list=Db::name("reporting")->where($map)->order(" status asc,sendtime desc,id desc")->paginate($pagesizes);
        $this->assign("pid",$pid);
        $this->assign("ty",$ty);
        $this->assign("arr",$arr);
        $this->assign("pagesizes",$pagesizes);
        $this->assign("list",$list);
        return $this->fetch("/message/reporting");
    }
    //审核
    public function report_audit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("reporting")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("reporting")->where("id=".$id)->update($data)){
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }
    //删除
    public function report_del()
    {
        $id=input('id',0);
        $v=Db::name("reporting")->find($id);
        if(Db::name("reporting")->delete($id)){
//        if(1){
            $url="/admin/message/reporting";
            if($v['pid'])$url.="/pid/".$v['pid'];
            if($v['ty'])$url.="/ty/".$v['ty'];
            if($v['tty'])$url.="/tty/".$v['tty'];
            if($v['ttty'])$url.="/ttty/".$v['ttty'];
            if(file_exists(substr($v['img1'],1))){
                unlink(substr($v['img1'],1));
            }
            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",$url);
        }else{
            $this->error("删除失败");
        }



    }
    //多选删除
    public function report_action()
    {

        $act=$_POST['act'];
//        $sorting_arr=$_POST['sorting'];
        $checkid=$_POST['checkid'];
//        print_r($checkid);
//        print_r($_POST);

      if($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("reporting")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }

    }



//    商家入驻
    public function settlement()
    {

        $q=input('q',"");
        $pagesize=input('pagesize',0);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        if($q) $map['title']=array('like',"%$q%");
        $list=Db::name("settlement")->where($map)->order("sendtime desc,id desc")->paginate($pagesizes);
        $this->assign("pagesizes",$pagesizes);
        $this->assign("list",$list);
        return $this->fetch("/message/settlement");
    }
    //审核
    public function settlement_audit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("settlement")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("settlement")->where("id=".$id)->update($data)){
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }
    //删除
    public function settlement_del()
    {
        $id=input('id',0);
        $v=Db::name("settlement")->find($id);
        if(Db::name("settlement")->delete($id)){
//        if(1){
            $url="/admin/message/settlement";
            if($v['pid'])$url.="/pid/".$v['pid'];
            if($v['ty'])$url.="/ty/".$v['ty'];
            if($v['tty'])$url.="/tty/".$v['tty'];
            if($v['ttty'])$url.="/ttty/".$v['ttty'];
            if(file_exists(substr($v['img1'],1))){
                unlink(substr($v['img1'],1));
            }
            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",$url);
        }else{
            $this->error("删除失败");
        }



    }
    //多选删除
    public function settlement_action()
    {

        $act=$_POST['act'];
        $checkid=$_POST['checkid'];
        if($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("settlement")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }

    }




//    一键投诉
    public function complaint()
    {

        $q=input('q',"");
        $pagesize=input('pagesize',0);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        if($q) $map['title']=array('like',"%$q%");
        $list=Db::name("complaint")->where($map)->order("sendtime desc,id desc")->paginate($pagesizes);
        $this->assign("pagesizes",$pagesizes);
        $this->assign("list",$list);
        return $this->fetch("/message/complaint");
    }
    //审核
    public function complaint_audit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("complaint")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("complaint")->where("id=".$id)->update($data)){
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }
    //删除
    public function complaint_del()
    {
        $id=input('id',0);
        $v=Db::name("complaint")->find($id);
        if(Db::name("complaint")->delete($id)){
//        if(1){
            $url="/admin/message/complaint";
            if(file_exists(substr($v['img1'],1))){
                unlink(substr($v['img1'],1));
            }
            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",$url);
        }else{
            $this->error("删除失败");
        }



    }
    //多选删除
    public function complaint_action()
    {

        $act=$_POST['act'];
        $checkid=$_POST['checkid'];
        if($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("complaint")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }

    }




//    我的建议
    public function proposal()
    {

        $q=input('q',"");
        $pagesize=input('pagesize',0);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        if($q) $map['title']=array('like',"%$q%");
        $list=Db::name("proposal")->where($map)->order("sendtime desc,id desc")->paginate($pagesizes);
        $this->assign("pagesizes",$pagesizes);
        $this->assign("list",$list);
        return $this->fetch("/message/proposal");
    }
    //审核
    public function proposal_audit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("proposal")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("proposal")->where("id=".$id)->update($data)){
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }
    //删除
    public function proposal_del()
    {
        $id=input('id',0);
        $v=Db::name("proposal")->find($id);
        if(Db::name("proposal")->delete($id)){
//        if(1){
            $url="/admin/message/proposal";
            if(file_exists(substr($v['img1'],1))){
                unlink(substr($v['img1'],1));
            }
            $this->sys_log("删除新闻:(id:$id)");
            $this->success("删除成功",$url);
        }else{
            $this->error("删除失败");
        }



    }
    //多选删除
    public function proposal_action()
    {

        $act=$_POST['act'];
        $checkid=$_POST['checkid'];
        if($act=='alldel') {
            for($i=0;$i<count($checkid);$i++) {
                $this->sys_log("删除信息:(id:$checkid[$i])");
                Db::name("proposal")->delete($checkid[$i]);
            }
            $this->success("信息删除成功",cookie('Jumpurl'));
        }

    }

}